package com.ruoyi.college.controller;

import java.util.List;

import com.ruoyi.college.domain.ExperimentReport;
import com.ruoyi.college.service.IExperimentReportService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.ExperimentClassProject;
import com.ruoyi.college.service.IExperimentClassProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生实验内容安排Controller
 * 
 * @author ruoyi
 * @date 2025-08-31
 */
@Controller
@RequestMapping("/college/studentExperimentProject")
public class ExperimentStudentProjectController extends BaseController
{
    private String prefix = "college/studentExperimentProject";

    @Autowired
    private IExperimentClassProjectService experimentClassProjectService;

    @RequiresPermissions("college:studentExperimentProject:view")
    @GetMapping()
    public String studentExperimentProject()
    {
        return prefix + "/studentExperimentProject";
    }

    /**
     * 查询学生实验内容安排列表
     */
    @RequiresPermissions("college:studentExperimentProject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExperimentClassProject experimentClassProject)
    {
        startPage();
        //学生只能看到启动的实验项目
        experimentClassProject.setStatus(1);
        List<ExperimentClassProject> list = experimentClassProjectService.selectExperimentClassProjectList(experimentClassProject);
        return getDataTable(list);
    }

    /**
     * 导出学生实验内容安排列表
     */
    @RequiresPermissions("college:studentExperimentProject:export")
    @Log(title = "学生实验内容安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExperimentClassProject experimentClassProject)
    {
        //学生只能看到启动的实验项目
        experimentClassProject.setStatus(1);
        List<ExperimentClassProject> list = experimentClassProjectService.selectExperimentClassProjectList(experimentClassProject);
        ExcelUtil<ExperimentClassProject> util = new ExcelUtil<ExperimentClassProject>(ExperimentClassProject.class);
        return util.exportExcel(list, "学生实验内容安排数据");
    }

    /**
     * 新增学生实验内容安排
     */
    @RequiresPermissions("college:studentExperimentProject:add")
    @GetMapping("/add/{classProjectId}")
    public String add(@PathVariable("classProjectId") Long classProjectId, ModelMap mmap)
    {
        ExperimentClassProject experimentClassProject = experimentClassProjectService.selectExperimentClassProjectByClassProjectId(classProjectId);
        mmap.put("experimentClassProject", experimentClassProject);
        return prefix + "/add";
    }

    /**
     * 新增保存学生实验内容安排
     */
    @RequiresPermissions("college:studentExperimentProject:add")
    @Log(title = "学生实验内容安排", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExperimentClassProject experimentClassProject)
    {


        return toAjax(experimentClassProjectService.insertExperimentClassProject(experimentClassProject));
    }

    @Autowired
    private IExperimentReportService experimentReportService;

    /**
     * 修改学生实验内容安排
     */
    @RequiresPermissions("college:studentExperimentProject:edit")
    @GetMapping("/edit/{classProjectId}")
    public String edit(@PathVariable("classProjectId") Long classProjectId, ModelMap mmap)
    {
        ExperimentClassProject experimentClassProject = experimentClassProjectService.selectExperimentClassProjectByClassProjectId(classProjectId);
        ExperimentReport experimentReport = new ExperimentReport();
        experimentReport.setClassProjectId(classProjectId);
        experimentReport.setStudentId(ShiroUtils.getUserId());
        List<ExperimentReport> experimentReports = experimentReportService.selectExperimentReportList(experimentReport);
        if (!CollectionUtils.isEmpty(experimentReports)){
            mmap.put("experimentReport", experimentReports.get(0));
            mmap.put("experimentClassProject", experimentClassProject);
            return prefix + "/edit";
        }
        mmap.put("experimentReport", new ExperimentReport());
        return prefix + "/edit";
    }

    /**
     * 修改保存学生实验内容安排
     */
    @RequiresPermissions("college:studentExperimentProject:edit")
    @Log(title = "学生实验内容安排", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExperimentClassProject experimentClassProject)
    {
        return toAjax(experimentClassProjectService.updateExperimentClassProject(experimentClassProject));
    }

    /**
     * 删除学生实验内容安排
     */
    @RequiresPermissions("college:studentExperimentProject:remove")
    @Log(title = "学生实验内容安排", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(experimentClassProjectService.deleteExperimentClassProjectByClassProjectIds(ids));
    }
}
